package com.tywj.panda.travel

import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.collection.ArrayMap
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.tywj.lib.core.common.extension.onClick
import com.tywj.lib.core.common.util.BarUtil
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.net.retrofit.request.BaseRequest
import com.tywj.panda.customer.common.entity.TravelEntity
import com.tywj.panda.customer.common.view.paging.BasePagingFragmentSearch
import com.tywj.panda.travel.api.TravelApi
import com.tywj.panda.travel.event.TravelEvent
import com.tywj.panda.travel.route.TravelRoute
import kotlinx.android.synthetic.main.travel_fragment_travel.*

/**
 * 行程
 */
@Route(path = TravelRoute.TRAVEL)
class TravelFragment :
    BasePagingFragmentSearch<TravelEntity, TravelEntity.DataBean>(
        TravelEntity.DataBean::class.java) {

    override fun getLayoutID(): Int = R.layout.travel_fragment_travel


    override fun getRecyclerViewAdapter(): BaseQuickAdapter<TravelEntity.DataBean, *> {
        return TravelSubAdapter()
    }

    //加载更多
    //说明，由于服务器不能把返回的时间放到返回的数据最外层，交给前端来处理，在上拉更多时需要把返回的最后一条数据的时间交给服务器。
    override fun getRequestParamsLoadMore(): ArrayMap<String, String> {
        return TravelApi.listTravelParams(mAdapter.data.last().lineDate)
    }

    override fun getRequest(): BaseRequest<*> = TravelApi.listTravel()

    override fun getRequestParams(): ArrayMap<String, String> {
        return TravelApi.listTravelParams()
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun initListener() {
        super.initListener()

        TravelEvent.loadTravelDataSuccessEvent.observe(this, Observer {

            mPagingHelper.refresh()
        })

        tvHistoryTravel.onClick {
            TravelRoute.toTravelHistory().navigation(this)
        }

        mAdapter.setOnItemChildClickListener { _, _, position ->
            val bean = mAdapter.data[position]

        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        with(statusLayout) {
            setEmptyLayoutIcon(R.drawable.common_data_is_empty)
            setEmptyLayoutTitle(R.string.travel_empty_tips)
        }
    }

    override fun onVisibleEveryTime() {
        super.onVisibleEveryTime()
        activity?.let {
            BarUtil.setStatusBarColor(it, ResourcesUtil.getColor(R.color.common_white))
            BarUtil.setStatusBarLightMode(it.window, true)
        }
    }

    override fun onLoadSuccess(isRefresh: Boolean, list: ArrayList<TravelEntity.DataBean>): Boolean {
        //数据拼接处理
        val listDate = ArrayList<String>()

        list.forEach {
            //如果行程包含了同样的时间，则不显示,只显示第一条时间
            val dateTravel = if (listDate.contains(it.lineDate)) {
                ""
            } else {
                it.lineDate
            }
            it.lineTravelDate = dateTravel

            listDate.add(it.lineTravelDate)

            it.listTravel.add(it)
        }

        if (isRefresh) {
            mAdapter.setNewData(list)
        } else {
            mAdapter.addData(list)
        }
        return true
    }
}